package com.bos.controller;

import com.bos.data.model.BosBehaviorModel;
import com.bos.data.model.BosParamModel;
import com.bos.data.model.BosUserModel;
import com.bos.service.Bos_behaviorService;
import com.bos.service.LogsService;
import com.bos.common.PageData;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class Bos_behaviorController {
    @Autowired
    @Qualifier("bos_behaviorServiceImpl")
    Bos_behaviorService behaviorService;
    @Autowired
    private LogsService logsService;

    //1.查询出Bos_behavior中的数据
    @RequestMapping("/findBehavior")
    public String findBehavior(Model model, HttpSession session, HttpServletRequest request) {
        //得到页面的当前页数和页面大小，默认为第一页
        String pageIndex = request.getParameter("pageNo");
        String setPageSize = request.getParameter("setPageSize");
        //给参数设置默认值
        int pageNo = 1, pageSize = PageData.DEFAULTPAGESIZE;
        if (!Strings.isNullOrEmpty(pageIndex)) {
            pageNo = Integer.valueOf(pageIndex);
        }
        if (!Strings.isNullOrEmpty(setPageSize)) {
            pageSize = Integer.valueOf(setPageSize);
        }

        PageData behavior = behaviorService.findBehavior(pageSize, pageNo);
        List<BosParamModel> pa = behaviorService.findBos_params("类别");
        List<BosParamModel> servious = behaviorService.findBos_params("严重度");
        List<BosUserModel> users= behaviorService.findUsers();
        session.setAttribute("pa", pa);
        model.addAttribute("list", behavior);
        session.setAttribute("servious", servious);
        session.setAttribute("users",users);
        return "record";

    }


    //2.将数据插入到行为记录的表中
    @RequestMapping(value = "addBehavior", method = RequestMethod.POST)
    public String addBehavior(Model model,@ModelAttribute BosBehaviorModel bosBehaviorModel, HttpSession session) throws Exception {
     /*   BosBehaviorModel behavior = new BosBehaviorModel();
        behavior.setBname(bosBehaviorModel.getBname());
        behavior.setbType(bosBehaviorModel.getbType());
        behavior.setCode(bosBehaviorModel.getCode());
        behavior.setRegular(bosBehaviorModel.getRegular());
        behavior.setbContent(bosBehaviorModel.getbContent());
        behavior.setbCondition(bosBehaviorModel.getbCondition());
        behavior.setbDate(bosBehaviorModel.getbDate());
        behavior.setServious(servious);
        behavior.setPeople(people);
        behavior.setCoin(coin);
        behavior.setRemark(remark);
        behavior.setOthers1(others1);
        behavior.setOthers2(others2);
        behavior.setOthers3(others3);*/
        int row = behaviorService.insertBehavior(bosBehaviorModel);
        //插入日志
        logsService.insertLogs(session, "添加行为记录");
        return "redirect:findBehavior";

    }

    //3.根据id删除行为表中的数据
    @RequestMapping(value = "/deleteBehaviorID")
    public String deleteBydId(@RequestParam("bid") int bid, HttpSession session) {
        int row = behaviorService.deleteById(bid);
        //插入日志
        logsService.insertLogs(session, "删除行为记录");
        return "redirect:findBehavior";

    }

    //4.进行导出Excel的操作
    @RequestMapping("/findExcel")
    public String findExcel(Model model) {
        List<BosBehaviorModel> behavior = behaviorService.findBehaviors();
        model.addAttribute("behavior", behavior);
        return "jsp/testExcel";
    }

    //5.根据id查询出表中的数据
    @RequestMapping(value = "/findByid/{bid}")
    public String findByID(@PathVariable("bid") int bid, Model model, HttpSession session) {
        //调用Service层的方法
        BosBehaviorModel behavior = behaviorService.findByBehavior(bid);
        model.addAttribute("be", behavior);
        List<BosParamModel> pa = behaviorService.findBos_params("类别");
        session.setAttribute("pa", pa);
        List<BosParamModel> servious = behaviorService.findBos_params("严重度");
        session.setAttribute("servious", servious);
        return "jsp/updatebehavior";

    }

    //6.根据id修改数据i
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public String updateById(@ModelAttribute BosBehaviorModel bosBehaviorModel, HttpSession session) {
        //接下来调用service层的方法
         BosBehaviorModel behavior = new BosBehaviorModel(bosBehaviorModel.getId(),bosBehaviorModel.getBname(), bosBehaviorModel.getbType(), bosBehaviorModel.getCode(), bosBehaviorModel.getRegular(), bosBehaviorModel.getbContent(), bosBehaviorModel.getbCondition(), bosBehaviorModel.getbDate(), bosBehaviorModel.getServious(), bosBehaviorModel.getPeople(), bosBehaviorModel.getCoin(), bosBehaviorModel.getRemark(), bosBehaviorModel.getOthers1(), bosBehaviorModel.getOthers2(), bosBehaviorModel.getOthers3());
        int row = behaviorService.updateByID(behavior);
        //插入日志
        logsService.insertLogs(session, "修改行为记录");
        return "redirect:findBehavior";
    }

    @RequestMapping(value = "copyBehaviour/{id}")
    public String copyBehaviour(@PathVariable("id") int id,HttpSession session){
        String name=behaviorService.copyBehaviour(id);
        //插入日志
        logsService.insertLogs(session, "复制一条行为记录数据,姓名: ["+name+"]");
        return "redirect:../findBehavior";
    }
}
